﻿using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using GSQ.BusinessEntities;
using GSQ.DataAccess.DataSetTableAdapters;

namespace GSQ.DataAccess
{
    public class FormasPagosDA
    {
        private FORMAS_PAGOSTableAdapter Adapter = new FORMAS_PAGOSTableAdapter();
        private DataSet.FORMAS_PAGOSDataTable entities;
        private DataSet.FORMAS_PAGOSRow entity;

        public decimal Insert(FORMAS_PAGOS value)
        {
            using (SqlConnection cnn = new SqlConnection(Connection.GetConnectionString))
            {
                try
                {
                    cnn.Open();
                    Adapter.Connection = cnn;

                    entities = new DataSet.FORMAS_PAGOSDataTable();

                    entity = entities.NewFORMAS_PAGOSRow();

                    entity.ID = value.ID;
                    entity.FORMA_PAGO = value.FORMA_PAGO.Trim();

                    entities.AddFORMAS_PAGOSRow(entity);
                    Adapter.Update(entities);

                    value.ID = entity.ID;
                    return value.ID;
                }
                finally
                {
                    if (cnn.State != System.Data.ConnectionState.Closed)
                        cnn.Close();
                }
            }
        }

        public decimal Update(FORMAS_PAGOS value)
        {
            using (SqlConnection cnn = new SqlConnection(Connection.GetConnectionString))
            {
                try
                {
                    cnn.Open();
                    this.Adapter.Connection = cnn;

                    entities = Adapter.GetDataByID(value.ID);
                    entity = entities.FirstOrDefault();

                    entity.FORMA_PAGO = value.FORMA_PAGO.Trim();

                    this.Adapter.Update(entities);

                    return entity.ID;
                }
                finally
                {
                    if (cnn.State != System.Data.ConnectionState.Closed)
                        cnn.Close();
                }
            }
        }

        public FORMAS_PAGOS GetById(decimal id)
        {
            using (SqlConnection cnn = new SqlConnection(Connection.GetConnectionString))
            {
                try
                {
                    cnn.Open();
                    this.Adapter.Connection = cnn;

                    FORMAS_PAGOS banco;

                    entity = this.Adapter.GetDataByID(id).FirstOrDefault();

                    if (entity != null)
                    {
                        banco = new FORMAS_PAGOS
                            {
                                ID = entity.ID,
                                FORMA_PAGO = entity.FORMA_PAGO,
                            };
                    }
                    else
                        banco = null;

                    return banco;
                }
                finally
                {
                    if (cnn.State != System.Data.ConnectionState.Closed)
                        cnn.Close();
                }
            }
        }

        public List<FORMAS_PAGOS> GetAll()
        {
            using (SqlConnection cnn = new SqlConnection(Connection.GetConnectionString))
            {
                try
                {
                    cnn.Open();
                    this.Adapter.Connection = cnn;

                    List<FORMAS_PAGOS> lst = new List<FORMAS_PAGOS>();
                    foreach (DataSet.FORMAS_PAGOSRow rw in this.Adapter.GetData())
                    {
                        lst.Add(new FORMAS_PAGOS
                        {
                            ID = rw.ID,
                            FORMA_PAGO = rw.FORMA_PAGO,
                        });
                    }
                    cnn.Close();
                    return lst;
                }
                finally
                {
                    if (cnn.State != System.Data.ConnectionState.Closed)
                        cnn.Close();
                }
            }
        }

        public List<FORMAS_PAGOS> GetByDescripcion(string value)
        {
            using (SqlConnection cnn = new SqlConnection(Connection.GetConnectionString))
            {
                try
                {
                    cnn.Open();
                    this.Adapter.Connection = cnn;

                    List<FORMAS_PAGOS> lst = new List<FORMAS_PAGOS>();
                    foreach (DataSet.FORMAS_PAGOSRow rw in this.Adapter.GetDataByFormaPago(value.Trim()))
                    {
                        lst.Add(new FORMAS_PAGOS
                        {
                            ID = rw.ID,
                            FORMA_PAGO = rw.FORMA_PAGO,
                            PLAZO = rw.PLAZO
                        });
                    }
                    return lst;
                }
                finally
                {
                    if (cnn.State != System.Data.ConnectionState.Closed)
                        cnn.Close();
                }
            }
        }
    }
}
